package com.wiscamp.ninechapters.tags.adapter.resources;

import com.wiscamp.ninechapters.tags.application.dto.command.CreateSubjectCommand;
import com.wiscamp.ninechapters.tags.application.dto.command.UpdateSubjectCommand;
import com.wiscamp.ninechapters.tags.application.dto.vo.SubjectVO;
import com.wiscamp.ninechapters.tags.application.services.SubjectAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 学科服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Tags")
@Tag(name = "标签知识点-学科信息")
public class SubjectController extends BaseController {

    private final SubjectAppService subjectAppService;

    public SubjectController(SubjectAppService subjectAppService) {
        this.subjectAppService = subjectAppService;
    }

    /**
     * 通过学科编号获取学科
     *
     * @param request
     * @param response
     * @param subjectId
     * @return
     */
    @GetMapping("Subjects/{subjectId}")
    @Operation(summary = "通过学科编号获取学科")
    @HandleResourceException
    public RestResponse<SubjectVO> getSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @PathVariable("subjectId") long subjectId) {
        return ResourceUtils.getById(request, response, subjectId, subjectAppService::getSubject);
    }

    /**
     * 获取全部学科列表
     *
     * @return 学科列表
     */
    @GetMapping("Subjects/Available")
    @Operation(summary = "获取可用学科列表")
    @HandleResourceException
    public RestResponse<Collection<SubjectVO>> getAvailableSubjects(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, subjectAppService::getAvailableSubjects);
    }

    /**
     * 通过学科代码获取学科
     *
     * @param subjectCode
     * @return
     */
    @GetMapping("Subjects/Code/{subjectCode}")
    @Operation(summary = "通过学科代码获取学科")
    @HandleResourceException
    public RestResponse<SubjectVO> getSubjectByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @PathVariable("subjectCode") String subjectCode) {
        return ResourceUtils.getByText(request, response, subjectCode, subjectAppService::getSubjectByCode);
    }


    /**
     * 通过学科名称获取学科列表
     *
     * @param subjectName
     * @return
     */
    @GetMapping("Subjects/Name/{subjectName}")
    @Operation(summary = "通过学科名称获取学科列表")
    @HandleResourceException
    public RestResponse<Collection<SubjectVO>> getSubjectsByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                 @PathVariable("subjectName") String subjectName) {
        return ResourceUtils.getListByText(request, response, subjectName, subjectAppService::getSubjectsByName);
    }


    /**
     * 通过学科编号删除学科
     *
     * @param command
     * @return
     */
    @PutMapping("Subjects/Actions/Delete")
    @Operation(summary = "通过学科编号删除学科")
    @HandleResourceException
    public RestResponse<Boolean> deleteSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, subjectAppService::deleteSubject);
    }

    /**
     * 通过学科编号启用学科
     *
     * @param command
     * @return
     */
    @PutMapping("Subjects/Actions/Enable")
    @Operation(summary = "通过学科编号启用学科")
    @HandleResourceException
    public RestResponse<Boolean> enableSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, subjectAppService::enableSubject);
    }

    /**
     * 通过学科编号禁用学科
     *
     * @param command
     * @return
     */
    @PutMapping("Subjects/Actions/Disable")
    @Operation(summary = "通过学科编号禁用学科")
    @HandleResourceException
    public RestResponse<Boolean> disableSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, subjectAppService::disableSubject);
    }

    /**
     * 创建学科
     *
     * @param command
     * @return
     */
    @PostMapping("Subjects")
    @Operation(summary = "创建学科")
    @HandleResourceException
    public RestResponse<SubjectVO> createSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody CreateSubjectCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, subjectAppService::createSubject);
    }

    /**
     * 更新学科
     *
     * @param command
     * @return
     */
    @PutMapping("Subjects")
    @Operation(summary = "更新学科")
    @HandleResourceException
    public RestResponse<SubjectVO> updateSubject(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody UpdateSubjectCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, subjectAppService::updateSubject);
    }
}
